package ihis.tms.client;

import ihis.IIHISTMSClient;
import ihis.core.IHISUtil;
import ihis.core.service.DefaultIoHandler;
import ihis.core.service.IServiceCategory;
import ihis.core.service.ServiceClient;
import ihis.model.GeneralFilter;
import ihis.model.Member;
import ihis.model.Performance;
import ihis.model.PredictedCurve;
import ihis.model.Project;
import ihis.model.Schedule;
import ihis.model.Task;
import ihis.model.TaskFilter;
import ihis.model.Team;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

public class IHISTMSClient implements IIHISTMSClient {
	private String hostName = "localhost";
	private int serverPort = 7403;
	private static Map<String, Member> userMap = new HashMap<String, Member>();
	private static Map<String, Team> teamMap = new HashMap<String, Team>();
	private static Map<String, Project> projectMap = new HashMap<String, Project>();
	/**
	 * @return the hostName
	 */
	public String getHostName() {
		return hostName;
	}

	/**
	 * @param hostName the hostName to set
	 */
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	/**
	 * @return the serverPort
	 */
	public int getServerPort() {
		return serverPort;
	}

	/**
	 * @param serverPort the serverPort to set
	 */
	public void setServerPort(int serverPort) {
		this.serverPort = serverPort;
	}

	@Override
	public String getProjectName(String projectId) {
		if(IHISUtil.isEmpty(projectId)) return "";
		
		if(projectMap.size() == 0){
			projectMap = new HashMap<String, Project>();
			Vector<Project> projects = getProjects();
			if(projects != null){
				for(Project p : projects){
					projectMap.put(p.getId(), p);
				}
			}
		}
		
		Project project = projectMap.get(projectId);
		if(project != null){
			return project.getName();
		} else {
			return "";
		}
		
		
//		String result = null;
//		Hashtable table = new Hashtable();
//		table.put("id", projectId);
//		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.PROJECT_NAME, table);
//		try {
//			result = (String)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return result;
	}

	@Override
	public String getTeamName(String teamId) {
		if(IHISUtil.isEmpty(teamId)) return "";

		if(teamMap.size() == 0){
			teamMap = new HashMap<String, Team>();
			Vector<Team> teams = getTeams();
			if(teams != null){
				for(Team t : teams){
					teamMap.put(t.getId(), t);
				}
			}
		}
		
		Team team = teamMap.get(teamId);
		if(team != null){
			return team.getName();
		} else {
			return "";
		}
		
		/*
		String result = null;
		Hashtable table = new Hashtable();
		table.put("id", teamId);
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.TEAM_NAME, table);
		try {
			result = (String)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
		*/
	}

	@Override
	public Vector<Task> getTaskList(TaskFilter filter) {
		// TODO Auto-generated method stub
		/** search filter 규약
		 *	- 일반 Task Search :
		 *		projectId - Project id
		 *		userId - owner id
		 *		teamId - team id
		 *		taskId - Task id, 
		 *		ownerFirstName - 담당자 이름
		 *		ownerLastName - 담당자 성
		 *		status	- Task 상태 (진행, 완료)
		 *		fromDueDateRange/toDueDateRange - Due date 기간
		 *
		 *	- Report별 Task Search : 
		 *		projectId - Project id
		 *		userId - owner id
		 *		teamId - team id
		 *		taskId - Task id
		 * 		startDate - 검색 기간
		 *  	periodType - 검색 기간 단위 (단위에 따라 검색 기간이 단위 첫날로 옮겨짐.
		 *  	ownerType - owner type
		 */
		Vector<Task> result = null;
		Hashtable table = new Hashtable();
		table.put("filter", filter);
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.TASK_LIST, table);
		try {
			result = (Vector<Task>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Vector<Schedule> getScheduleList(GeneralFilter filter) {
		/** search filter 규약
		 *  	periodType - 검색 기간 단위 (단위에 따라 검색 기간이 단위 첫날로 옮겨짐.
		 *  	startDAte - 검색 시간
		 *  	ownerType - owner type
		 */
		Vector<Schedule> result = null;
		Hashtable table = new Hashtable();
		table.put("filter", filter);
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.SCHEDULE_LIST, table);
		try {
			result = (Vector<Schedule>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Vector<Task> getRiskList() {
		Vector<Task> result = null;
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.RISK_LIST, new Hashtable());
		try {
			result = (Vector<Task>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Vector<Performance> getPerformanceList(GeneralFilter filter) {
		/** search filter 규약
		 *  	periodType - 검색 기간 단위 (단위에 따라 검색 기간이 단위 첫날로 옮겨짐.
		 *  	ownerType - owner type
		 */
		Vector<Performance> result = null;
		Hashtable table = new Hashtable();
		table.put("filter", filter);
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.PERFORMANCE_LIST, table);
		try {
			result = (Vector<Performance>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Vector<PredictedCurve> getPredictedCurveList(GeneralFilter filter) {
		/** search filter 규약
		 *  	periodType - 검색 기간 단위 (단위에 따라 검색 기간이 단위 첫날로 옮겨짐.
		 *  	ownerType - owner type
		 */
		Vector<PredictedCurve> result = null;
		Hashtable table = new Hashtable();
		table.put("filter", filter);
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.PREDICTED_CURVE_LIST, table);
		try {
			result = (Vector<PredictedCurve>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String getUserName(String userId) {
		if(IHISUtil.isEmpty(userId)) return "";

		if(userMap.size() == 0){
			userMap = new HashMap<String, Member>();
			Vector<Member> users = getUsers();
			if(users != null){
				for(Member m : users){
					userMap.put(m.getId(), m);
				}
			}
		}
		
		Member member = userMap.get(userId);
		if(member != null){
			return member.getName();
		} else {
			return "";
		}
		
		/*
		String result = null;
		Hashtable table = new Hashtable();
		table.put("id", userId);
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.USER_NAME, table);
		try {
			result = (String)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
		*/	
	}

	@Override
	public Vector<Team> getTeams() {
		// TODO Auto-generated method stub
		Vector<Team> result = null;
		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.TEAMS, new Hashtable());
		try {
			result = (Vector<Team>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Vector<Project> getProjects() {
		Vector<Project> result = null;

		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.PROJECTS, new Hashtable());
		try {
			result = (Vector<Project>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public Vector<Member> getUsers() {
		// TODO Auto-generated method stub
		Vector<Member> result = null;

		DefaultIoHandler ioHandler = new DefaultIoHandler(IServiceCategory.USERS, new Hashtable());
		try {
			result = (Vector<Member>)ServiceClient.serviceCall(hostName, serverPort, ioHandler);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;	
	}
	
	@Override
	public Member getUser(String userId) {
		if(IHISUtil.isEmpty(userId)) return null;
		
		if(userMap.size() == 0){
			userMap = new HashMap<String, Member>();
			Vector<Member> users = getUsers();
			if(users != null){
				for(Member m : users){
					userMap.put(m.getId(), m);
				}
			}
		}
		
		Member member = userMap.get(userId);
		if(member != null){
			return member;
		} else {
			return null;
		}
	}

}
